<?php
/*********************************************
  Dragonfly CMS, Copyright (c) 2004 by DragonflyCMS Dev. Team.
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/

# http://www.php.net/manual/en/filter.filters.validate.php
abstract class Filter
{
	public static function _get()
	{
		// Resolve LEO generated URI's
		$query = URL::query();
		if (!empty($_GET) && false === strpos($query, '?')) {
			if (false !== strpos($query, '.')) {
				$_SERVER['REDIRECT_STATUS'] = 404;
			}
			// parse module name
			$qs = explode('/', str_replace('.html', '', $query));
			if (preg_match('#^[a-zA-Z0-9_]+$#i', $qs[0])) {
				$qs[0] = 'name='.$qs[0];
			}
			// parse module file
			if (!empty($qs[1]) && preg_match('#^([a-zA-Z0-9_]+)$#', $qs[1])) {
				$qs[1] = 'file='.$qs[1];
			}
			// push query string into $_GET
			$_GET = array();
			parse_str(implode('&', $qs), $_GET);
		}
	}

	public static function username($var) {}
	public static function hostname($var) {}

	/*
		@ Return bool
	*/
	public static function email($email) {
		# Although RFC 1035 doesn't allow 1 char subdomains we allow it due to bug report 641
		return 1 === preg_match('#^[\w\.\+\-]+@(([\w]{1,25}\.)?[0-9a-z\-]{2,63}\.[a-z]{2,6}(\.[a-z]{2,6})?)$#', $email);
	}

	/*
		@ Return bool
	*/
	public static function domain($www)
	{
		if (false === strpos($www, '://')) {
			$www = (preg_match('#^([a-z0-9\-\.]+)?[a-z0-9\-]+\.[a-z]{2,4}\:443$#i', $www) ? 'https://' : 'http://') .$www;
		}
		return 1 === preg_match('#^http[s]?\:\/\/([a-z0-9\-\.]+)?[a-z0-9\-]+\.[a-z]{2,4}$#i', $www);
	}
}
